/*
 * Copyright 2002-2006 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.riceman.simpleGo.view.views;

import java.awt.BorderLayout;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.DefaultCaret;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.richclient.application.support.AbstractView;

import de.riceman.simpleGo.SimpleGo;
import de.riceman.simpleGo.events.SimpleGoLifecycleApplicationEvent;
import de.riceman.simpleGo.main.BitMatrix;
import de.riceman.simpleGo.main.GoGame;
import de.riceman.simpleGo.players.FirstSimpleAI;
import de.riceman.simpleGo.players.Player;

/**
 * This class defines the initial view to be presented in the SimpleGo application. It is
 * constructed automatically by the platform and configured according to the bean
 * specification in the application context.
 * 
 * @author Larry Streepy
 * 
 */
public class AIView extends AbstractView implements ApplicationListener {

	private static final Log logger = LogFactory.getLog(AIView.class);
	
	private GoGame goGame;
	private JScrollPane scrollPane;
	private JTextArea console;
    /**
     * Create the actual UI control for this view. It will be placed into the window
     * according to the layout of the page holding this view.
     */
    protected JComponent createControl() {
        // In this view, we're just going to use standard Swing to place a
        // few controls.

    	console = new JTextArea();
    	console.setEditable(false);
    	console.setAutoscrolls(true);
    	
    	DefaultCaret caret = (DefaultCaret) console.getCaret();
    	caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    	
    	scrollPane = new JScrollPane(console, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);


        JPanel panel = getComponentFactory().createPanel(new BorderLayout());
        panel.add(scrollPane);

        return panel;
    }

	@Override
	public void onApplicationEvent(ApplicationEvent event) {

		if (event instanceof SimpleGoLifecycleApplicationEvent) {
			
			StringBuffer out = new StringBuffer();
			
			SimpleGoLifecycleApplicationEvent lcae = (SimpleGoLifecycleApplicationEvent) event;
			
			if ((lcae.getEventType() == SimpleGoLifecycleApplicationEvent.INITIALIZED) && lcae.objectIs(GoGame.class)) {
				
				goGame = (GoGame) lcae.getObject();
			}
			
			if (lcae.getEventType() == SimpleGoLifecycleApplicationEvent.MOVE_EXPECTED && lcae.objectIs(Player.class)) {
				
				Player player = (Player) lcae.getObject();
				
				out.append("\n__________________________\nMOVE No. " + goGame.getMoveCount() + "\n");
				
				if (player.getPlayer() && player instanceof FirstSimpleAI) {
					out.append("dark player's turn> FirstSimpleAI is about to calculate next move...\n");
				}
				if (!player.getPlayer() && player instanceof FirstSimpleAI) {
					out.append("light player's turn> FirstSimpleAI is about to calculate next move...\n");
				}
			}

			if (lcae.getEventType() == SimpleGoLifecycleApplicationEvent.AI_THINKS_OUT_LOUD && lcae.objectIs(String.class)) {
		    	String message = (String) lcae.getObject();

		    	out.append(message);
		    }
			
			console.append(out.toString());
		}
	}


	/**
	 * @param move
	 * @return the coordinate as a readable String
	 */
	private String getCoordinate(BitMatrix move) {
		// TODO Auto-generated method stub
		int i = move.getIndexOfFirstSetBit();
		int a;
		
		if (i == -1) {
			return "PASSED\n";
		}
		
		a = i / goGame.getConfiguration().getSize();
		i = i % goGame.getConfiguration().getSize();
		
		return "(" + Integer.toString(a) + "/" + Integer.toString(i) + ")\n";
	}
}
